from selenium.webdriver.common.by import By
from base.base import Base
from util.number_util import get_all_number


class Page_Home(Base):


    logo_locator = (By.XPATH,'//div[@class="ele-admin-logo"]')
    oter_app_locator = (By.XPATH,'//div[@class="ant-image"]')
    top_data_locator = (By.CLASS_NAME,'analysis-chart-card-num')
    mid_data_locator = (By.CLASS_NAME,'analysis-trend-text')
    alarm_list_locator = (By.XPATH,'//td[contains(@class,"ant-table-cell")]')
    site_number_locator = (By.XPATH,'//label[contains(@class,"ant-radio-button-wrapper")]')
    site_data_locator = (By.CLASS_NAME,'scene-item')
    foot_page_locator = (By.XPATH,'//div[contains(text(),"共") and contains(text(),"条")]')

    def jump_click_logo(self):
        self.click(self.logo_locator)

    def jump_other_app(self):
        self.click(self.oter_app_locator)

    def get_topdata(self):
        values = []
        for elm in self.finds(self.top_data_locator):
            values.append(int(self.get_text_elm(elm)))
        return values
        
    def get_middata(self):
        values = []
        for elm in self.finds(self.mid_data_locator):
            values.append(self.get_text_elm(elm))
        return get_all_number(values)
    
    def get_alarmlist(self):
        values = []
        for elm in self.finds(self.alarm_list_locator):
            values.append(self.get_text_elm(elm))
        return values
    
    def get_site_number(self):
        elms = self.finds(self.site_number_locator)
        values = []
        for elm in elms:
            values.append(int(self.get_text_elm(elm).split('(')[1].split(')')[0]))
        return values
        
    def get_site_data(self,id = 1):
        elms = self.finds(self.site_number_locator)
        values = []
        newvalues = []
        page_number = []
        for index,elm in enumerate(elms):
            elm.click()
            if self.get_site_number()[index] == 0:
                page_number.append('0')
                continue
            else:
                page_number.append(self.get_text(self.foot_page_locator))
                data_elms = self.finds(self.site_data_locator)
                for data_elm in data_elms:
                    values.append(self.get_text_elm(data_elm).split('\n'))
        for key in values:
            for vindex,i in enumerate(key):
                if vindex%11 == 1:
                    newvalues.append(i.split('：')[1])
                elif vindex%11 in [0,2,3,5,7,9]:
                    newvalues.append(i)
        if id == 1:
            return newvalues
        elif id == 2:
            return get_all_number(page_number)

